package com.example.redis.topic;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.util.concurrent.CountDownLatch;

@Configuration
public class RedisMessageListener {

    /**
     * redis消息监听器容器
     *
     * @param connectionFactory
     * @param priceListenerAdapter       价格趋势和市场消息订阅处理器
     * @param listingListenerAdapter     挂牌案例消息订阅处理器
     * @param caseListenerAdapter        交易案例消息订阅处理器
     * @param foreclosureListenerAdapter 法拍案例消息订阅处理器
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter priceListenerAdapter,
                                            MessageListenerAdapter listingListenerAdapter,
                                            MessageListenerAdapter caseListenerAdapter,
                                            MessageListenerAdapter foreclosureListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //监听价格趋势和市场情况主题并绑定消息订阅处理器
        container.addMessageListener(priceListenerAdapter, new PatternTopic("PRICE_TOPIC"));
        //监听挂牌案例主题并绑定消息订阅处理器
        container.addMessageListener(listingListenerAdapter, new PatternTopic("LISTING_TOPIC"));
        //监听交易案例主题并绑定消息订阅处理器
        container.addMessageListener(caseListenerAdapter, new PatternTopic("CASE_TOPIC"));
        //监听法拍案例主题并绑定消息订阅处理器
        container.addMessageListener(foreclosureListenerAdapter, new PatternTopic("FORECLOSURE_TOPIC"));
        return container;
    }

    /**
     * 价格趋势和市场消息订阅处理器,并指定处理方法
     *
     * @param receiver
     * @return
     */
    @Bean
    MessageListenerAdapter priceListenerAdapter(ReceiverRedisMessage receiver) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver, "priceReceive");
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = this.jacksonSerializer();
        messageListenerAdapter.setSerializer(jackson2JsonRedisSerializer);
        return messageListenerAdapter;
    }

    /**
     * 挂牌案例消息订阅处理器,并指定处理方法
     *
     * @param receiver
     * @return
     */
    @Bean
    MessageListenerAdapter listingListenerAdapter(ReceiverRedisMessage receiver) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver, "listingReceive");
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = this.jacksonSerializer();
        messageListenerAdapter.setSerializer(jackson2JsonRedisSerializer);
        return messageListenerAdapter;
    }


    /**
     * 交易案例消息订阅处理器,并指定处理方法
     *
     * @param receiver
     * @return
     */
    @Bean
    MessageListenerAdapter caseListenerAdapter(ReceiverRedisMessage receiver) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver, "caseReceive");
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = this.jacksonSerializer();
        messageListenerAdapter.setSerializer(jackson2JsonRedisSerializer);
        return messageListenerAdapter;
    }


    /**
     * 法拍案例消息订阅处理器,并指定处理方法
     *
     * @param receiver
     * @return
     */
    @Bean
    MessageListenerAdapter foreclosureListenerAdapter(ReceiverRedisMessage receiver) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver, "foreclosureReceive");
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = this.jacksonSerializer();
        messageListenerAdapter.setSerializer(jackson2JsonRedisSerializer);
        return messageListenerAdapter;
    }

    @Bean
    ReceiverRedisMessage receiver(CountDownLatch latch) {
        return new ReceiverRedisMessage(latch);
    }

    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    @Bean
    RedisTemplate template(RedisTemplate redisTemplate) {
        return redisTemplate;
    }

    private Jackson2JsonRedisSerializer jacksonSerializer() {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }
}

